package com.visionet.school.app.web.service.member;

import com.visionet.school.app.web.entity.member.Member;
import com.visionet.school.app.web.entity.member.MemberRel;
import com.visionet.school.app.web.entity.vo.MemberVo;
import com.visionet.school.app.web.repository.member.MemberRelRepository;
import com.visionet.school.app.web.repository.member.MemberRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by hxpeng on 2017/3/3.
 */
@Service
public class MemberRelService {

    private static final Logger logger = LoggerFactory.getLogger(MemberRelService.class);

    @Resource
    private MemberRelRepository memberRelRepository;

    @Resource
    private MemberRepository memberRepository;

    public MemberRel findOneByParent(String relMemberId,String relType){return memberRelRepository.findOneByParent(relMemberId,relType);}

    public int studentHadBind(String memberId){return memberRelRepository.studentHadBind(memberId);}

    public List<MemberVo> getFamilyInfo(String relMemberId){
        if (relMemberId!=null){
            Member student = memberRepository.findOne(relMemberId);
            List<MemberVo> list =  memberRelRepository.AllfamilyInfo(student.getId());
            return list;
        }
        return null;
    }

    public  void updateReltype(String relType,String relmemberId,String memberId){
        Member member=memberRepository.findOne(relmemberId);
        Date date=new Date();
        try {
            MemberRel memberRelInfo = memberRelRepository.findOneBymidAndsid(memberId,relmemberId);
            MemberRel memberRel=memberRelRepository.findOne(memberRelInfo.getId());
            memberRel.setRelType(relType);
            memberRel.setCreateBy(member.getMemberName());
            memberRel.setCreateDate(date);
            memberRelRepository.save(memberRel);
        }catch (Exception e){
            throw new RuntimeException("更新亲属关系失败："+ e.getMessage());
        }
    }

}
